

// 不能通过地址跳转，只能通过登陆跳转
let admin=sessionStorage.getItem("admin");
if(!admin){
    location.replace("/ad");
}



// 获取用户信息
function xinXi() {
    $.ajax({
        type: "GET",
        url: "http://localhost:3000/ad/user",
        data: {
            cons1: 1
        },
        dataType: "json",
        success: function (data) {
            let str = ""
            data.data.forEach((item, index) => {
                str += `
                        <tr>
                            <th>
                                <input class="check" type="radio" index="${index}">
                            </th>
                            <th>${data.cons1 == 1 ? index + 1 : index + (7 * (data.cons - 1)) + 1}</th>
                            <td>${item.username}</td>
                            <td><img src=".${item.url}" style="width: 55px; height: 38px;" alt=""></td>
                            <td>${item.tel}</td>
                            <td>${item.status == 1 ? "正常" : "冻结"}</td>
                            <td>${item.time}</td>
                            <td>${item.times}</td>
                            <td>
                            <button type="button" class="btn btn-outline-primary dong" ${item.status == 1 ? "" : "style='display: none;'"} index="${item.id}">冻结</button>
                            <button type="button" class="btn btn-outline-success qi" ${item.status == 1 ? "style='display: none;'" : ""} index="${item.id}">启用</button>
                            <button type="button" class="btn btn-outline-danger desl" index="${item.id}">注销</button>
                            </td>
                        </tr>
                `
            });
            $(".tas").html(str);
            // 总用户数
            $(".num").text(data.nums);
            // 总页数
            $(".zong").text(data.cons);
            // 当前页数
            $(".dang").text(data.cons1);
            let check = document.getElementsByClassName("check");
            let datas = data.data;

            // 分页首页
            $(".shou").click(function () {
                $(".inp").val("")
                $.ajax({
                    type: "GET",
                    url: "http://localhost:3000/ad/user",
                    data: {
                        cons1: 1
                    },
                    dataType: "json",
                    success: function (data) {
                        let str = ""
                        data.data.forEach((item, index) => {
                            str += `
                                    <tr>
                                        <th>
                                            <input class="check" type="radio" index="${index}">
                                        </th>
                                        <th>${data.cons1 == 1 ? index + 1 : index + (7 * (data.cons1 - 1)) + 1}</th>
                                        <td>${item.username}</td>
                                        <td><img src=".${item.url}" style="width: 55px; height: 38px;" alt=""></td>
                                        <td>${item.tel}</td>
                                        <td>${item.status == 1 ? "正常" : "冻结"}</td>
                                        <td>${item.time}</td>
                                        <td>${item.times}</td>
                                        <td>
                                        <button type="button" class="btn btn-outline-primary dong" ${item.status == 1 ? "" : "style='display: none;'"} index="${item.id}">冻结</button>
                                        <button type="button" class="btn btn-outline-success qi" ${item.status == 1 ? "style='display: none;'" : ""} index="${item.id}">启用</button>
                                        <button type="button" class="btn btn-outline-danger desl" index="${item.id}">注销</button>
                                        </td>
                                    </tr>
                            `
                        });
                        $(".tas").html(str);
                        // 总用户数
                        $(".num").text(data.nums);
                        // 总页数
                        $(".zong").text(data.cons);
                        // 当前页数
                        $(".dang").text(data.cons1);
                        check = document.getElementsByClassName("check");
                        datas = data.data;

                        // 冻结用户
                        $(".dong").click(function () {
                            let id = $(this).attr("index")
                            $.ajax({
                                type: "GET",
                                url: "/ad/dong",
                                data: {
                                    id
                                },
                                dataType: "json",
                                success: function (data) {
                                    if (data.code == 1) {
                                        $(".shou").click();
                                    }
                                }
                            })
                        })


                        // 解冻用户
                        $(".qi").click(function () {
                            let id = $(this).attr("index")
                            $.ajax({
                                type: "GET",
                                url: "/ad/qi",
                                data: {
                                    id
                                },
                                dataType: "json",
                                success: function (data) {
                                    if (data.code == 1) {
                                        $(".shou").click();
                                    }
                                }
                            })
                        })

                        // 注销账号
                        $(".desl").click(function () {
                            let id = $(this).attr("index")
                            $.ajax({
                                type: "GET",
                                url: "/ad/desl",
                                data: {
                                    id
                                },
                                dataType: "json",
                                success: function (data) {
                                    if (data.code == 1) {
                                        $(".shou").click();
                                    }
                                }
                            })
                        })


                    }

                });
            })

            // 分页尾页
            $(".wei").click(function () {
                $(".inp").val("")
                $.ajax({
                    type: "GET",
                    url: "http://localhost:3000/ad/user",
                    data: {
                        cons1: data.cons
                    },
                    dataType: "json",
                    success: function (data) {
                        let str = ""
                        data.data.forEach((item, index) => {
                            str += `
                                    <tr>
                                        <th>
                                            <input class="check" type="radio" index="${index}">
                                        </th>
                                        <th>${data.cons1 == 1 ? index + 1 : index + (7 * (data.cons1 - 1)) + 1}</th>
                                        <td>${item.username}</td>
                                        <td><img src=".${item.url}" style="width: 55px; height: 38px;" alt=""></td>
                                        <td>${item.tel}</td>
                                        <td>${item.status == 1 ? "正常" : "冻结"}</td>
                                        <td>${item.time}</td>
                                        <td>${item.times}</td>
                                        <td>
                                        <button type="button" class="btn btn-outline-primary dong" ${item.status == 1 ? "" : "style='display: none;'"} index="${item.id}">冻结</button>
                                        <button type="button" class="btn btn-outline-success qi" ${item.status == 1 ? "style='display: none;'" : ""} index="${item.id}">启用</button>
                                        <button type="button" class="btn btn-outline-danger desl" index="${item.id}">注销</button>
                                        </td>
                                    </tr>
                            `
                        });
                        $(".tas").html(str);
                        // 总用户数
                        $(".num").text(data.nums);
                        // 总页数
                        $(".zong").text(data.cons);
                        // 当前页数
                        $(".dang").text(data.cons1);
                        check = document.getElementsByClassName("check");
                        datas = data.data;
                        // 冻结用户
                        $(".dong").click(function () {
                            let id = $(this).attr("index")
                            $.ajax({
                                type: "GET",
                                url: "/ad/dong",
                                data: {
                                    id
                                },
                                dataType: "json",
                                success: function (data) {
                                    if (data.code == 1) {
                                        $(".wei").click();
                                    }
                                }
                            })
                        })


                        // 解冻用户
                        $(".qi").click(function () {
                            let id = $(this).attr("index")
                            $.ajax({
                                type: "GET",
                                url: "/ad/qi",
                                data: {
                                    id
                                },
                                dataType: "json",
                                success: function (data) {
                                    if (data.code == 1) {
                                        $(".wei").click();
                                    }
                                }
                            })
                        })

                        // 注销账号
                        $(".desl").click(function () {
                            let id = $(this).attr("index")
                            $.ajax({
                                type: "GET",
                                url: "/ad/desl",
                                data: {
                                    id
                                },
                                dataType: "json",
                                success: function (data) {
                                    if (data.code == 1) {
                                        $(".wei").click();
                                    }
                                }
                            })
                        })


                    }

                });
            })

            // 上一页
            $(".shang").click(function () {
                $(".inp").val("")

                let nums = Number($(".dang").text()) - 1;

                if (nums <= 0) {
                    $.ajax({
                        type: "GET",
                        url: "http://localhost:3000/ad/user",
                        data: {
                            cons1: data.cons
                        },
                        dataType: "json",
                        success: function (data) {
                            let str = ""
                            data.data.forEach((item, index) => {
                                str += `
                                        <tr>
                                            <th>
                                                <input class="check" type="radio" index="${index}">
                                            </th>
                                            <th>${data.cons1 == 1 ? index + 1 : index + (7 * (data.cons1 - 1)) + 1}</th>
                                            <td>${item.username}</td>
                                            <td><img src=".${item.url}" style="width: 55px; height: 38px;" alt=""></td>
                                            <td>${item.tel}</td>
                                            <td>${item.status == 1 ? "正常" : "冻结"}</td>
                                            <td>${item.time}</td>
                                            <td>${item.times}</td>
                                            <td>
                                            <button type="button" class="btn btn-outline-primary dong" ${item.status == 1 ? "" : "style='display: none;'"} index="${item.id}">冻结</button>
                                            <button type="button" class="btn btn-outline-success qi" ${item.status == 1 ? "style='display: none;'" : ""} index="${item.id}">启用</button>
                                            <button type="button" class="btn btn-outline-danger desl" index="${item.id}">注销</button>
                                            </td>
                                        </tr>
                                `
                            });
                            $(".tas").html(str);
                            // 总用户数
                            $(".num").text(data.nums);
                            // 总页数
                            $(".zong").text(data.cons);
                            // 当前页数
                            $(".dang").text(data.cons1);
                            datas = data.data;
                            check = document.getElementsByClassName("check");

                            // 冻结用户
                            $(".dong").click(function () {
                                let id = $(this).attr("index")
                                $.ajax({
                                    type: "GET",
                                    url: "/ad/dong",
                                    data: {
                                        id
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == 1) {
                                            $(".shou").click();
                                        }
                                    }
                                })
                            })


                            // 解冻用户
                            $(".qi").click(function () {
                                let id = $(this).attr("index")
                                $.ajax({
                                    type: "GET",
                                    url: "/ad/qi",
                                    data: {
                                        id
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == 1) {
                                            $(".shou").click();
                                        }
                                    }
                                })
                            })

                            // 注销账号
                            $(".desl").click(function () {
                                let id = $(this).attr("index")
                                $.ajax({
                                    type: "GET",
                                    url: "/ad/desl",
                                    data: {
                                        id
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == 1) {
                                            $(".shou").click();
                                        }
                                    }
                                })
                            })

                        }

                    });
                    $(".dang").text(data.cons)

                } else {
                    $.ajax({
                        type: "GET",
                        url: "http://localhost:3000/ad/user",
                        data: {
                            cons1: nums
                        },
                        dataType: "json",
                        success: function (data) {
                            let str = ""
                            data.data.forEach((item, index) => {
                                str += `
                                        <tr>
                                            <th>
                                                <input class="check" type="radio" index="${index}">
                                            </th>
                                            <th>${data.cons1 == 1 ? index + 1 : index + (7 * (data.cons1 - 1)) + 1}</th>
                                            <td>${item.username}</td>
                                            <td><img src=".${item.url}" style="width: 55px; height: 38px;" alt=""></td>
                                            <td>${item.tel}</td>
                                            <td>${item.status == 1 ? "正常" : "冻结"}</td>
                                            <td>${item.time}</td>
                                            <td>${item.times}</td>
                                            <td>
                                            <button type="button" class="btn btn-outline-primary dong" ${item.status == 1 ? "" : "style='display: none;'"} index="${item.id}">冻结</button>
                                            <button type="button" class="btn btn-outline-success qi" ${item.status == 1 ? "style='display: none;'" : ""} index="${item.id}">启用</button>
                                            <button type="button" class="btn btn-outline-danger desl" index="${item.id}">注销</button>
                                            </td>
                                        </tr>
                                `
                            });
                            $(".tas").html(str);
                            // 总用户数
                            $(".num").text(data.nums);
                            // 总页数
                            $(".zong").text(data.cons);
                            // 当前页数
                            $(".dang").text(data.cons1);
                            datas = data.data;
                            check = document.getElementsByClassName("check");
                            // 冻结用户
                            $(".dong").click(function () {
                                let id = $(this).attr("index")
                                $.ajax({
                                    type: "GET",
                                    url: "/ad/dong",
                                    data: {
                                        id
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == 1) {
                                            $(".shou").click();
                                        }
                                    }
                                })
                            })


                            // 解冻用户
                            $(".qi").click(function () {
                                let id = $(this).attr("index")
                                $.ajax({
                                    type: "GET",
                                    url: "/ad/qi",
                                    data: {
                                        id
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == 1) {
                                            $(".shou").click();
                                        }
                                    }
                                })
                            })

                            // 注销账号
                            $(".desl").click(function () {
                                let id = $(this).attr("index")
                                $.ajax({
                                    type: "GET",
                                    url: "/ad/desl",
                                    data: {
                                        id
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == 1) {
                                            $(".shou").click();
                                        }
                                    }
                                })
                            })

                        }

                    });
                    $(".dang").text(nums)
                }

            })


            // 下一页
            $(".xia").click(function () {
                $(".inp").val("")
                let nums = Number($(".dang").text()) + 1;
                if (nums > data.cons) {
                    $.ajax({
                        type: "GET",
                        url: "http://localhost:3000/ad/user",
                        data: {
                            cons1: 1
                        },
                        dataType: "json",
                        success: function (data) {
                            let str = ""
                            data.data.forEach((item, index) => {
                                str += `
                        <tr>
                            <th>
                                <input class="check" type="radio" index="${index}">
                            </th>
                            <th>${data.cons1 == 1 ? index + 1 : index + (7 * (data.cons1 - 1)) + 1}</th>
                            <td>${item.username}</td>
                            <td><img src=".${item.url}" style="width: 55px; height: 38px;" alt=""></td>
                            <td>${item.tel}</td>
                            <td>${item.status == 1 ? "正常" : "冻结"}</td>
                            <td>${item.time}</td>
                            <td>${item.times}</td>
                            <td>
                            <button type="button" class="btn btn-outline-primary dong" ${item.status == 1 ? "" : "style='display: none;'"} index="${item.id}">冻结</button>
                            <button type="button" class="btn btn-outline-success qi" ${item.status == 1 ? "style='display: none;'" : ""} index="${item.id}">启用</button>
                            <button type="button" class="btn btn-outline-danger desl" index="${item.id}">注销</button>
                            </td>
                        </tr>
                `
                            });
                            $(".tas").html(str);
                            // 总用户数
                            $(".num").text(data.nums);
                            // 总页数
                            $(".zong").text(data.cons);
                            // 当前页数
                            $(".dang").text(data.cons1);
                            datas = data.data;
                            check = document.getElementsByClassName("check");
                            // 冻结用户
                            $(".dong").click(function () {
                                let id = $(this).attr("index")
                                $.ajax({
                                    type: "GET",
                                    url: "/ad/dong",
                                    data: {
                                        id
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == 1) {
                                            $(".shou").click();
                                        }
                                    }
                                })
                            })


                            // 解冻用户
                            $(".qi").click(function () {
                                let id = $(this).attr("index")
                                $.ajax({
                                    type: "GET",
                                    url: "/ad/qi",
                                    data: {
                                        id
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == 1) {
                                            $(".shou").click();
                                        }
                                    }
                                })
                            })

                              // 注销账号
                              $(".desl").click(function () {
                                let id = $(this).attr("index")
                                $.ajax({
                                    type: "GET",
                                    url: "/ad/desl",
                                    data: {
                                        id
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == 1) {
                                            $(".shou").click();
                                        }
                                    }
                                })
                            })
                        }

                    });
                    $(".dang").text("1")
                } else {
                    $.ajax({
                        type: "GET",
                        url: "http://localhost:3000/ad/user",
                        data: {
                            cons1: nums
                        },
                        dataType: "json",
                        success: function (data) {
                            let str = ""
                            data.data.forEach((item, index) => {
                                str += `
                                        <tr>
                                            <th>
                                                <input class="check" type="radio" index="${index}">
                                            </th>
                                            <th>${data.cons1 == 1 ? index + 1 : index + (7 * (data.cons1 - 1)) + 1}</th>
                                            <td>${item.username}</td>
                                            <td><img src=".${item.url}" style="width: 55px; height: 38px;" alt=""></td>
                                            <td>${item.tel}</td>
                                            <td>${item.status == 1 ? "正常" : "冻结"}</td>
                                            <td>${item.time}</td>
                                            <td>${item.times}</td>
                                            <td>
                                                <button type="button" class="btn btn-outline-primary dong" ${item.status == 1 ? "" : "style='display: none;'"} index="${item.id}">冻结</button>
                                                <button type="button" class="btn btn-outline-success qi" ${item.status == 1 ? "style='display: none;'" : ""} index="${item.id}">启用</button>
                                                <button type="button" class="btn btn-outline-danger desl" index="${item.id}">注销</button>
                                            </td>
                                        </tr>
                                `
                            });
                            $(".tas").html(str);
                            // 总用户数
                            $(".num").text(data.nums);
                            // 总页数
                            $(".zong").text(data.cons);
                            // 当前页数
                            $(".dang").text(data.cons1);
                            datas = data.data;
                            check = document.getElementsByClassName("check");
                            // 冻结用户
                            $(".dong").click(function () {
                                let id = $(this).attr("index")
                                $.ajax({
                                    type: "GET",
                                    url: "/ad/dong",
                                    data: {
                                        id
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == 1) {
                                            $(".shou").click();
                                        }
                                    }
                                })
                            })


                            // 解冻用户
                            $(".qi").click(function () {
                                let id = $(this).attr("index")
                                $.ajax({
                                    type: "GET",
                                    url: "/ad/qi",
                                    data: {
                                        id
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == 1) {
                                            $(".shou").click();
                                        }
                                    }
                                })
                            })


                            // 注销账号
                            $(".desl").click(function () {
                                let id = $(this).attr("index")
                                $.ajax({
                                    type: "GET",
                                    url: "/ad/desl",
                                    data: {
                                        id
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == 1) {
                                            $(".shou").click();
                                        }
                                    }
                                })
                            })



                        }

                    });
                    $(".dang").text(nums)
                }

            })


            // 单选

            $(".check").click(function () {
                this.checked = false
                for (let i = 0; i < check.length; i++) {
                    check[i].checked = false;
                    this.checked = true
                }

            })

            // 修改用户信息
            $(".xius").click(function () {
                let newche = Array.from(check);
                let checks = newche.find(item => {
                    return item.checked;
                })
                if (checks) {
                    let index = checks.getAttribute("index")
                    let values = JSON.stringify(datas[index])
                    console.log(values);
                    localStorage.setItem("values", values)
                    location.href = "/admin/user_update.html"
                } else {
                    alert("您未选择用户")
                }
            })

            // 冻结用户
            $(".dong").click(function () {
                let id = $(this).attr("index")
                $.ajax({
                    type: "GET",
                    url: "/ad/dong",
                    data: {
                        id
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 1) {
                            $(".shou").click();
                        }
                    }
                })
            })


            // 解冻用户
            $(".qi").click(function () {
                let id = $(this).attr("index")
                $.ajax({
                    type: "GET",
                    url: "/ad/qi",
                    data: {
                        id
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 1) {
                            $(".shou").click();
                        }
                    }
                })
            })

            // 注销账号
            $(".desl").click(function () {
                let id = $(this).attr("index")
                $.ajax({
                    type: "GET",
                    url: "/ad/desl",
                    data: {
                        id
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 1) {
                            $(".shou").click();
                        }
                    }
                })
            })


        }
    })
}
xinXi();


// 模糊查询
$(".sou").click(function () {
    let content = $(".inp").val();
    $.ajax({
        type: "GET",
        url: "/ad/sou",
        data: {
            content
        },
        dataType: "json",
        success: function (data) {
            let str = ""
            data.forEach((item, index) => {
                str += `
        <tr>
            <th>
                <input class="check" type="radio">
            </th>
            <th>${index + 1}</th>
            <td>${item.username}</td>
            <td><img src=".${item.url}" style="width: 55px; height: 38px;" alt=""></td>
            <td>${item.tel}</td>
            <td>${item.status == 1 ? "正常" : "冻结"}</td>
            <td>${item.time}</td>
            <td>${item.times}</td>
            <td>
                <button type="button" class="btn btn-outline-primary" ${item.status == 1 ? "" : "style='display: none;'"} index="${item.id}">冻结</button>
                <button type="button" class="btn btn-outline-success" ${item.status == 1 ? "style='display: none;'" : ""} index="${item.id}">启用</button>
                <button type="button" class="btn btn-outline-danger" index="${item.id}">注销</button>
            </td>
        </tr>
`
            });
            $(".tas").html(str);
            // 总用户数
            $(".num").text(data.length);
            // 总页数
            $(".zong").text("1");
            // 当前页数
            $(".dang").text("1");
        }
    })

})



